package com.example.pageradapterdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.pageradapterdemo.adapter.DemoAdapter;
import com.example.pageradapterdemo.adapter.DemoFragmentAdapter;
import com.example.pageradapterdemo.fragment.Page0Fragment;

import java.util.ArrayList;
import java.util.List;

public class ViewPager2Demo1Activity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private DemoFragmentAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager2_demo1);
        viewPager = findViewById(R.id.viewPager);

        List<String> mList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mList.add(String.valueOf(i));
        }
        adapter = new DemoFragmentAdapter(this);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(mList.size() - 1);

        Button btnAdd = findViewById(R.id.btnAddFragment);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.addFragment(new Page0Fragment());
            }
        });

        Button btnRemove = findViewById(R.id.btnRemove);
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.removeFragment();
            }
        });

        Button btnOrientation = findViewById(R.id.btnOrientation);
        if (viewPager.getOrientation() == ViewPager2.ORIENTATION_HORIZONTAL) {
            btnOrientation.setText("方向(垂直)");
        }else{
            viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
            btnOrientation.setText("方向(水平)");
        }

        btnOrientation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewPager.getOrientation() == ViewPager2.ORIENTATION_HORIZONTAL) {
                    viewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
                    btnOrientation.setText("方向(水平)");
                }else{
                    viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
                    btnOrientation.setText("方向(垂直)");
                }
            }
        });
    }
}